/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "frm_control_TDouble.h"

using namespace frm;
using namespace control;

// TDouble ////////////////////////////////////////////////////////////////////

//
void TDouble::SetValue(svl::TVariant V) 
{ 
  if (V.IsNull()) 
    { SetNull(); } 
  else 
    { SetValue(V.Double()); } 
}

//
char* TDouble::ConvertDoubleToString( double d, int p ){
	static char buff[80];
	svl::TString spec;
	spec << "%." << p << 'f';
	sprintf_s( buff, 80, spec, d );
	return buff;
}

//
void TDouble::SetValue(double V) 
{ 
  TString::SetValue( ConvertDoubleToString( V, Precision ) );
}  

//
void TDouble::SetPrecision(int P) 
{ 
  double temp = TString::GetValue().ConvertToDouble();
  Precision = P;
  
  TString::SetValue( ConvertDoubleToString( temp, Precision ) );
}  

//
bool TDouble::UpdateValue(svl::TString V)
{
  return TString::UpdateValue(V);
}

//
void TDouble::TComponent_KeyboardChar(grf::TKeyboardChar &KeyboardChar)
{
  if( !enableEdit )
	  return;

  if (KeyboardChar.Key == '-')
  {
    svl::TString V = TString::GetValue(); if (V[0] != '-')
    { 
      BeginPaint();
      V.AddToLeft(1,'-'); UpdateValue(V); 
      GetFocus().SetDeltaPos(1);
      EndPaint();
    }
    return;
  }
  else if (KeyboardChar.Key == '+')
  {
    svl::TString V = TString::GetValue(); if (V[0] == '-')
    { 
      BeginPaint();
      V.DelSym(0); UpdateValue(V); 
      GetFocus().SetDeltaPos(-1);
      EndPaint();
    }
    return;
  }
  else if (KeyboardChar.Key == '.')
  {
    int N; if (TString::GetValue().SearchSym('.',N)) { return;}
  }
  else if (!::isdigit(KeyboardChar.Key)) 
    { return;}
  else 
  {
    svl::TString V = TString::GetValue(); 
    V.InsSym(Focus_GetPos(),KeyboardChar.Key);

    if (V.Len() > 1)
    { 
	  double Vd = atof(V);
	  svl::TString Sd = ConvertDoubleToString( Vd, Precision ); 
      
      if (!Sd.CompareIC(V))
      { 
        BeginPaint();
		Focus_EnterByPos(Focus_GetPos()+1);
		
		if(Sd[Focus_GetPos()]=='.')
		  Focus_EnterByPos(Focus_GetPos()+1);
		
		UpdateValue(Sd); 
        EndPaint();
        return; 
      }
	}   
  }
  TString::TComponent_KeyboardChar(KeyboardChar);
}

//
void TDouble::TComponent_FocusExit()
{
  svl::TString V = TString::GetValue(); if (V.Len() == 1 && (V[0] == '-' || V[0] == '.'))
    { UpdateValue("");}
  TString::TComponent_FocusExit();
}

//
void TDouble::TComponent_KeyboardKey(grf::TKeyboardKey &KeyboardKey)
{
  int Pos = Focus_GetPos();

  if (KeyboardKey.State == grf::TKeyboardKey::EState_Down)
  {
    svl::TString V = TString::GetValue();
	if (KeyboardKey.Key == VK_LEFT && Pos > 0)
    {
	  if( V[Pos-1] != '.' )
	  {
	    if (Focus_EnterByPos(Pos-1))
          { Focus_FixCaretX(); }
	  }	
	  else
        if (Focus_EnterByPos(Pos-2))
          { Focus_FixCaretX(); }

      return;
    }
    else if (KeyboardKey.Key == VK_HOME && Pos > 0)
    {
      if (Focus_EnterByPos(0))
        { Focus_FixCaretX(); }
      return;
    }
    else if (KeyboardKey.Key == VK_RIGHT && Pos < V.Len())
    {
    if( V[Pos+1] != '.' )
	  {
	    if (Focus_EnterByPos(Pos+1))
          { Focus_FixCaretX(); }
	  }	
	  else
        if (Focus_EnterByPos(Pos+2))
          { Focus_FixCaretX(); }

      return;}
    else if (KeyboardKey.Key == VK_END && Pos < V.Len())
    {
      if (Focus_EnterByPos(V.Len()))
        { Focus_FixCaretX(); }
      return;
    }
    else if (KeyboardKey.Shift.IsEmpty() && KeyboardKey.Key == VK_DELETE && Pos < V.Len())
    {
      if ( V[Pos]=='.' ) return;
	  svl::TString Sym,SEdit = V; SEdit.DelSubStr(Pos,1,Sym);
	  int a =Focus_GetPos();
	  if ( SEdit[Focus_GetPos()]=='.' )
	    Focus_EnterByPos(Focus_GetPos()+1);
	  UpdateValue(SEdit);
	  if( SEdit[0]=='.')
        Focus_EnterByPos(Focus_GetPos()+1);
	  return;
    }
    else if (KeyboardKey.Key == VK_BACK && Pos > 0)
    {
      if ( V[Pos-1]=='.' ) return;
	  
	  svl::TString Sym,SEdit = V; SEdit.DelSubStr(Pos-1,1,Sym);
      
      if (UpdateValue(SEdit)) { if (Focus_EnterByPos(Pos-1)) { Focus_FixCaretX(); } }
      return;
    }
  }
  TPanel::TComponent_KeyboardKey(KeyboardKey);
}